// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router';
import App from './App'
import axios from 'axios';
import routes from './routes';
import Config from './config/config';
import store from './vuex/store';
import iView from 'iview';
import VueLazyload from 'vue-lazyload'
import 'iview/dist/styles/iview.css';    // 使用 CSS
import 'iview/dist/iview.min.js';
Vue.use(VueLazyload)


Vue.use(VueRouter);

Vue.use(iView);

Vue.config.productionTip = false;

// 路由配置
const router = new VueRouter({
  routes: routes,
  mode: Config.mode
})
Vue.use(VueLazyload,{
	loading:require('./assets/images/f.jpg')	
})
// axios配置
axios.defaults.baseURL = Config.api

axios.interceptors.request.use(config => {
	config.headers.common['Authorization'] = 'Bearer ' + window.localStorage.getItem('ccm-token');
	return config;

}, error => {
	// Do something with request error
	return Promise.reject(error);

});

// 响应拦截
axios.interceptors.response.use(response => {
	return response;

}, error => {

	if(error.response) {

		// 微信网页授权认证失败
		if(error.response.status == 401) {
			console.log(error);
		}

		// 需要登录的接口，跳转到登录注册页面
		if(error.response.status == 402) {
      		console.log(error);
		}

	} else {

	}

	return Promise.reject(error);
});

Vue.prototype.$http = axios

// 验证token微信授权
router.beforeEach((to, from, next) => {
	let url_token = to.query.token;
	let token = window.localStorage.getItem('ccm-token');
	if(url_token){
		window.localStorage.setItem("ccm-token", url_token);
	}else if(!url_token && !token) {
		// 没有token跳转到网页授权接口
		window.localStorage.setItem("ccm-token", url_token);
		next({
			path: to.fullPath
		})
	}
	next()
});



new Vue(Vue.util.extend({ router, store }, App)).$mount('#app-box');
